package com.xc.service.impl;

import com.xc.dto.ResultDTO;
import com.xc.dto.StudentDTO;
import com.xc.dto.UserDTO;
import com.xc.entity.StudentEntity;
import com.xc.entity.UserEntity;
import com.xc.mapper.StudentMapper;
import com.xc.mapper.UserMapper;
import com.xc.service.StudentService;
import com.xc.service.UserService;
import com.xc.util.JSONCopy;
import com.xc.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.UUID;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public ResultDTO add(StudentEntity studentEntity) {
        Assert.notNull(studentEntity.getName(),"学生姓名不能为空");
//        根据id查询是否有数据，如果有，则直接不用管数据，直接拿到数据存入数据库即可，如果没有，则添加数据时增加UUID
        List<StudentDTO> byUuid = studentMapper.findByUuid(studentEntity);
        if (byUuid==null||byUuid.size()==0) {
            studentEntity.setUuid(UUID.randomUUID().toString());
        }
        studentEntity.setPassword(MD5.encoderByMd5(studentEntity.getPassword()));
        return ResultDTO.builder()
                .code(0)
                .count(studentMapper.add(studentEntity))
                .build();
    }

    @Override
    public ResultDTO update(StudentEntity studentEntity) {
        studentEntity.setPassword(MD5.encoderByMd5(studentEntity.getPassword()));
        return ResultDTO.builder()
                .code(0)
                .count(studentMapper.update(studentEntity))
                .build();
    }

    @Override
    public ResultDTO delete(StudentEntity studentEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(studentMapper.delete(studentEntity))
                .build();
    }

    @Override
    public ResultDTO updateEnable(StudentEntity studentEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(studentMapper.updateEnable(studentEntity))
                .build();
    }

    @Override
    public ResultDTO findById(StudentEntity studentEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(studentMapper.findByIdCount(studentEntity))
                .data(studentMapper.findById(studentEntity))
                .build();
    }

    @Override
    public ResultDTO findAll(StudentEntity studentEntity) {

        return ResultDTO.builder()
                .code(0)
                .count(studentMapper.count(studentEntity))
                .data(studentMapper.findAll(studentEntity))
                .build();
    }

    @Override
    public ResultDTO count(StudentEntity studentEntity) {
        return ResultDTO.builder()
                .code(0)
                .code(0)
                .count(studentMapper.count(studentEntity))
                .build();
    }

    @Override
    public ResultDTO findByName(StudentEntity studentEntity) {
        return null;
    }

    @Override
    public ResultDTO findByUuid(StudentEntity studentEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(studentMapper.findByUuidCount(studentEntity))
                .data(studentMapper.findByUuid(studentEntity))
                .build();
    }
}
